/**
 * Created by levy on 16/7/6.
 */
{
    const lf = liefeng,
        RC = React.createClass,
        ajax = lf.utils.ajax;
    
    /**
     * BlockBtn 单行块状按钮, 带箭头
     * params:
     *      data: {
     *      tit: 最左侧的文字
     *      default: 最右侧的文字(如限3人), 不传则需要传 method
     *      selectHandler
     *      selectFlag: 用于区分显示BottomForm
     *      }
     *      method: 代表api, 根据它获取人员名字, 有默认值 lf.api.task.defaultAuditor
     *      
     * @args [selectFlag]
     */
    var BlockBtn = RC({
        getInitialState: function () {
            return {
                default: this.props.data.default || '',
                loginid: this.props.data.selectId || ''
            }
        },
        getDefaultProps: function () {
            return {
                loginid: '',
                visible: '',
                method: lf.api.task.defaultAuditor,//levy 默认审批人
                selectFlag: ''
            }
        },
        componentWillMount: function () {
            var props = this.props;
            //获取默认人员
            !props.data.default && ajax({
                data: {
                    eventId: lf.taskInfo.eventId,
                    //当前是办理，则找派工的;当前是审核，则找派单的
                    taskName: props.currTask.taskName == 'handle' ? 'dispatching' : 'distributeLeaflets',
                },
                success: function cb ( data ) {
                    //log(data.id)
                    var dt = data;
                    this.setState({
                        default: dt.name,
                        loginid: dt.id
                    });

                }.bind(this)
            }, props.method);
        },
        componentDidMount: function () {
            var me = this,
                dt = this.props.data;
            React.findDOMNode(this.refs.blockBtn).addEventListener('tap', function () {
                dt.selectHandler && dt.selectHandler( dt.selectFlag );
            }, !1);
            dt.selectFlag && setTimeout(function () {
                $('.J_select_'+ dt.selectFlag).find('.text-dot')
                    .each(function ( i, v ){
                        var data_id = v.getAttribute('data-id');
                        if ( me.state.loginid == data_id ) {
                            v.className = v.className + ' choose';
                            return false;
                        }
                    });
            }, 500);
        },
        render: function () {
            var props = this.props,
                state = this.state,
                dt = props.data,
                selectClass = dt.selectFlag ? dt.selectFlag : 'block-link';
            
            //select-id 在这里 levy
            return (
                <a ref="blockBtn" href="javascript:" className={props.visible + " block-btn-arrow "+ selectClass} data-selectid={state.loginid}>
                    <article>
                        <h2>{dt.tit}</h2>
                        <aside>
                            <p className="text-before-arrow">{state.default}</p>
                        </aside>
                    </article>
                </a>
            )
        }
    });
}
export default BlockBtn;


